package com.kkb.filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录限制
 */
@WebFilter(urlPatterns = "/*")
public class LoginFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        String uri = request.getRequestURI();
        if (!uri.contains("login") && (uri.endsWith(".html") || uri.endsWith(".jsp"))){
            Object user = request.getSession().getAttribute("user");
            if (user == null){
                // 未登录，不允许进入其他页面
                response.sendRedirect("/login.html");
            }
        }
        super.doFilter(request, response, chain);
    }
}
